package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.ProjectConstants;
import com.ruoyi.system.domain.SysProjectInfo;
import com.ruoyi.system.service.ISysProjectInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysTracking;
import com.ruoyi.system.service.ISysTrackingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实施跟踪Controller
 * 
 * @author riddou
 * @date 2022-04-19
 */
@RestController
@RequestMapping("/system/tracking")
public class SysTrackingController extends BaseController
{
    @Autowired
    private ISysTrackingService sysTrackingService;

    @Autowired
    private ISysProjectInfoService sysProjectInfoService;

    /**
     * 查询实施跟踪列表
     */
    @PreAuthorize("@ss.hasPermi('system:tracking:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTracking sysTracking)
    {
        startPage();
        List<SysTracking> list = sysTrackingService.selectSysTrackingList(sysTracking);
        return getDataTable(list);
    }

    /**
     * 导出实施跟踪列表
     */
    @PreAuthorize("@ss.hasPermi('system:tracking:export')")
    @Log(title = "实施跟踪", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTracking sysTracking)
    {
        List<SysTracking> list = sysTrackingService.selectSysTrackingList(sysTracking);
        ExcelUtil<SysTracking> util = new ExcelUtil<SysTracking>(SysTracking.class);
        util.exportExcel(response, list, "实施跟踪数据");
    }

    /**
     * 获取实施跟踪详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tracking:query')")
    @GetMapping(value = "/{trackId}")
    public AjaxResult getInfo(@PathVariable("trackId") Long trackId)
    {
        return AjaxResult.success(sysTrackingService.selectSysTrackingByTrackId(trackId));
    }

    /**
     * 新增实施跟踪
     */
    @PreAuthorize("@ss.hasPermi('system:tracking:add')")
    @Log(title = "实施跟踪", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTracking sysTracking)
    {
        SysProjectInfo project = sysProjectInfoService.selectSysProjectInfoByProjectId(sysTracking.getProjectId());
        if (project == null) return error("当前没有项目，无法实时跟踪");
        //如果不是实施跟踪阶段，则更新
        if (!ProjectConstants.STAGE_TRACKING.equals(project.getStage())) {
            project.setStage(ProjectConstants.STAGE_TRACKING);
            sysProjectInfoService.updateSysProjectInfo(project);
        }
        return toAjax(sysTrackingService.insertSysTracking(sysTracking));
    }

    /**
     * 修改实施跟踪
     */
    @PreAuthorize("@ss.hasPermi('system:tracking:edit')")
    @Log(title = "实施跟踪", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTracking sysTracking)
    {
        return toAjax(sysTrackingService.updateSysTracking(sysTracking));
    }

    /**
     * 删除实施跟踪
     */
    @PreAuthorize("@ss.hasPermi('system:tracking:remove')")
    @Log(title = "实施跟踪", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trackIds}")
    public AjaxResult remove(@PathVariable Long[] trackIds)
    {
        return toAjax(sysTrackingService.deleteSysTrackingByTrackIds(trackIds));
    }
}
